package irri.soils.ssnm;

import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

public class ActivityM extends SSNM implements TextWatcher{
	private Bundle b;
	private NoImeEditText kg;
	private TextView question;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.m);
        setCustomTitle();
        b = this.getIntent().getExtras();
        question = (TextView) findViewById(R.id.question);
        question.setText(get("kg0"));
        
        kg = (NoImeEditText) findViewById(R.id.kg);
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText(get("kgprompt"));
        kg.addTextChangedListener(this);
        if(b.containsKey("kg")) kg.setText(""+b.getInt("kg"));
        
        mInputView = (KeyboardView) findViewById(R.id.keyboard);
        
        mInputView.setOnKeyboardActionListener(this);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.number);
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
    
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityM.this, 
			(b.containsKey("basin")
			? ActivityK.class : b.getInt("INS")==1 ? (
			b.getString("varName").equals(get("variety22")) ? 
			ActivityI.class : (b.getInt("CE")==1 ? 
			ActivityH.class : ActivityG.class)) : ActivityJ.class));
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	@Override
	public void afterTextChanged(Editable s) {
		String kgTxt = ""+kg.getText();
		int kgVal = kgTxt.equals("") ? 0 : Integer.parseInt(kgTxt);
		if(kgVal>70){
			kg.setText(""); 
			kg.append(kgTxt.substring(0, kgTxt.length()-1));
			kgVal = kgVal / 10;
		}
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}

	/* (non-Javadoc)
	 * @see irri.soils.ssnm.SSNMActivity#onKey(int, int[])
	 */
	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		View v = this.getCurrentFocus();
		try{
			TextView tv = (TextView) v;
			String str = ""+tv.getText();
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {
				onDelete(tv, str);
			}
			else if(primaryCode == 10){
				try{
					Intent intent = new Intent(ActivityM.this, ActivityN.class);
					int kgVal = Integer.parseInt(""+kg.getText());
					if(kgVal<30) return;
					if(b.getInt("kg")!=kgVal){
						b.remove("cav");
						b.remove("cav2");
					}
					b.putInt("kg", kgVal);
					intent.putExtras(b);
					startActivity(intent);
					finish();
				}catch(Exception ex){
					
				}
			}
			else{
				onDigit(tv, str, primaryCode);
			}
		}catch(Exception ex){
			
		}
	}
}